// 新增商家
$("#addBusinessBtn").on("click", function () {
    var bnAuthorName = $(".bnAuthorName").val();
    var bnName = $(".bnName").val();
    if (bnAuthorName != null && bnAuthorName.length != 0) {
        if (bnName != null && bnName.length != 0) {
            // 判断用户名是否存在
            if (bnAuthorName != null && bnAuthorName.length != 0) {
                $.ajax({
                    type: "GET",
                    url: "/userManage/getUserByUserName?userName=" + bnAuthorName,
                    dataType: "json",
                    success: function (data) {
                        if (data.user != null) {
                            $(".bnAuthorId").val(data.user.userId);
                            $.ajax({
                                type: "POST",
                                url: "/businessManage/addBusiness",
                                dataType: "json",
                                data: $("#addBusinessForm").serialize(),
                                success: function (data) {
                                    if (data.result == "success") {
                                        alert("新增成功");
                                        window.location.reload();
                                    } else {
                                        alert("更新失败");
                                    }
                                },
                                error: function () {
                                    alert("服务器错误，操作失败");
                                }
                            });
                        } else {
                            alert("请重新输入认证人，该认证人不存在");
                        }
                    },
                    error: function () {
                        alert("服务器错误，操作失败");
                    }
                });
            }
        } else {
            alert("商家名称不能为空");
        }
    } else {
        alert("认证人不能为空，必且须是用户");
    }
});

// 搜索商家
$("#serarchBusinessBtn").on("click", function () {
    var type = $(".searchType").val();
    var content = $(".searchContent").val();
    if (content != null && content.length != 0) {
        window.location.href = "/businessManage/searchBusiness?type=" + type + "&content=" + content;
    } else {
        alert("请输入查询条件");
    }
});

// 更新商家信息
$("#updateBusinessBtn").on("click", function () {
    var bnName = $(".bnName").val();
    if (bnName != null && bnName.length != 0) {
        $.ajax({
            type: "POST",
            url: "/businessManage/updateBusiness",
            dataType: "json",
            data: $('#updateBusinessForm').serialize(),
            success: function (data) {
                if (data.result == "success") {
                    alert("更新成功");
                    window.location.reload();
                } else {
                    alert("更新失败");
                }
            },
            error: function () {
                alert("服务器错误，操作失败");
            }
        });
    } else {
        alert("请输入商家名称");
    }
});

// 上传主图
$(".bnMainPictureFile").bind("change", function () {
    var data = uploadfile("bnMainPictureFile");
    if (data.code == 100) {
        alert(data.msg);
    } else {
        console.log(data.path);
        $(".bnMainPicture").val(data.path);
        $(".bnMainPictureImg").attr("src", data.path);
    }
});

// 上传商家展示图
$(".bnExhibitionPictureFile").bind("change", function () {
    var data = uploadfile("bnExhibitionPictureFile");
    if (data.code == 100) {
        alert(data.msg);
    } else {
        console.log(data.path);
        $(".bnExhibitionPicture").val(data.path);
        $(".bnExhibitionPictureImg").attr("src", data.path);
    }
});

// 上传营业执照
$(".bnLicenseFile").bind("change", function () {
    var data = uploadfile("bnLicenseFile");
    if (data.code == 100) {
        alert(data.msg);
    } else {
        console.log(data.path);
        $(".bnLicense").val(data.path);
        $(".bnLicenseImg").attr("src", data.path);
    }
});

// 删除商家
function delBusinessFun(bnId) {
    $("#deleteBusinessBtn").on("click", function () {
        $.ajax({
            type: "GET",
            url: "/businessManage/deleteBusiness?bnId=" + bnId,
            dataType: "json",
            success: function (data) {
                if (data.result == "success") {
                    alert("删除成功");
                    window.location.reload();
                } else {
                    alert("删除失败");
                }
            },
            error: function () {
                alert("服务器错误，操作失败");
            }
        });
    });
}